<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="system.bean.ModuleDocument" %>
<%@page import="util.PageList"%>
<%@page import="system.bean.ModuleFile"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
ArrayList<ModuleDocument> myDocList = null;
ArrayList<ModuleFile> refList   = null;
PageList pageBean = (PageList)request.getAttribute("pageBean");
if(null != request.getAttribute("dosList") ){
	myDocList = (ArrayList<ModuleDocument>)request.getAttribute("dosList");
}
if(null != request.getAttribute("refList") ){
	refList = (ArrayList<ModuleFile>)request.getAttribute("refList");
}
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>NETCONF Development Toolkit</title>
    <meta http-equiv="Pragma" content="no-cache" />
    <meta http-equiv="Cache-Control" content="no-cache" />
    <meta http-equiv="Expires" content="0" />
	<link rel="stylesheet" type="text/css" href="<%=basePath%>/css/index.css" />
	<link rel="stylesheet" type="text/css" href="<%=basePath%>/css/items.css" />
	<link rel="stylesheet" type="text/css" href="<%=basePath%>/css/subItems.css" />
    <script type="text/javascript"  src="<%=basePath%>jstree-v.pre1.0/_lib/jquery.js"></script>
    <script type="text/javascript"  src="<%=basePath%>js/StringUtil.js"></script>
	
  </head>
  
  <body style="margin:0px; padding:0px;">
<!-- <div id="titleContent" class="titleContent">
  <div class="title_bar">
    <div style="float:left;width:300px"><img width="300" height="50" src="images/log_3.png" /></div>
    <div style="float:left;width:800px" >
       <div style="font-size:14px;height:32px;margin:0px;padding-top:2px;border:0px;">
          
         
       </div>
       <div align="right" style="font-size:14px;margin:0px;padding:0px;border:0px;">
           <a href="GoToIndexServlet">返回首页</a>&nbsp;&nbsp;&nbsp;
           <a target="_balck" href="membercenter/changepwd.jsp">我的帐户</a>&nbsp;&nbsp;&nbsp;
           <a href="admin/adminIndex.jsp">后台管理</a>&nbsp;&nbsp;&nbsp;  
           <a href="login.jsp?op=loginOut">注销</a>&nbsp;&nbsp;&nbsp;
           <a target="_balck" href="downloadfiles.jsp">文档下载</a>&nbsp;&nbsp;&nbsp;
       </div>
    </div>
    <div style="clear:both;"></div>
   </div>
 </div> --> 
   <jsp:include page="navigater.jsp"></jsp:include>
    <div class="mainContent">
	     <div class="itemTitle">
	          Create A New Module
	     </div>
	      <form id="CreateNewModuleForm"   method="post">
	      <div class="ListItem">
	      	 <span class="textSpan"> Module Name:</span>
	      	 <select  style="width:100px;   height:23px;" onchange="getModuleType(this.value)"   class="inputSelect" id="_moduleType" name="_moduleType">
	      	 	<option value="MainModule" selected="selected">MainModule</option>
	      	 	<option value="SubModule">SubModule</option>
	      	 </select>
	      	 <span class="textSpan" id="MainModuleSpan" style="display:none;width:200px;"> 
	      	 <select  style="width:200px;   height:23px;"   class="inputSelect" id="_mainModule" name="_mainModule">
	      	 </select>
	      	 </span>
	      	 <input class="inputText" style=" height:18px;"  id="ModuleName" name="ModuleName" /> 
	      	 <input type="button" name="Submit" value="Create" onclick="createNewModule()" /> 
	      	 
	      </div>
	      <% if(null != request.getAttribute("NewModuleMsg")) { %>
	 	  <div class="ListItem" align = "center">
	 	  	 <span class="noticeSpan"> <%=request.getAttribute("NewModuleMsg") %> </span>
	 	  </div>
		  <% } %>
	      </form>
    	<div class="itemTitle"> Open Document </div>
	      <% if(null != myDocList ){
	            Iterator<ModuleDocument> it = myDocList.iterator();
	             while(it.hasNext()){
	                 ModuleDocument  mDoc = it.next();
	      %>
	      <div class="ListItem">
			   <span class="ListSpan"><%=mDoc.getDisplayName() %></span>
			   <span class="ListSpan"></span>
			   <a href="OpenDocumentServlet?_docId=<%=mDoc.getDocId() %>" >
			   		<img class="listDocImg" src="<%=basePath%>images/open_document.jpg" />Open
			   </a>&nbsp;&nbsp;
			   <a href="ExportDocumentServlet?_fileType=xml&_docId=<%=mDoc.getDocId() %>" >
			   		<img class="listDocImg" src="<%=basePath%>images/export_document.jpg" />Xml
			   </a>&nbsp;&nbsp;
			   <a href="ExportDocumentServlet?_fileType=yang&_docId=<%=mDoc.getDocId() %>" >
			   		<img class="listDocImg" src="<%=basePath%>images/export_document.jpg" />Yang
			   </a>&nbsp;&nbsp;
			   <% if(mDoc.getStatus().equalsIgnoreCase("0")){ %>
			   <a href="#" onclick = "deleteDocment(<%=mDoc.getDocId() %>)" >
			   		<img class="listDocImg" src="<%=basePath%>images/delete1.jpg" />Delete
			   </a>
			    <%} %> 
		  </div>
	      <%    
	            }
	       } 
	       %>
         <% if(null != pageBean) {%>
	     <div class="ListItem">
	       <div  style="text-align:right;">
	       <%= PageList.getnumpagelist("GoToIndexServlet","1=1",pageBean) %>
	       </div>
	     </div>
	     <%} %>
	      <% if(null != request.getAttribute("msgList")) { %>
		 <div class="ListItem" align = "center">
		 	  <span class="noticeSpan"> <%=request.getAttribute("msgList") %> </span>
		 </div>
		 <% } %>
	     <div class="itemTitle">
	          Upload Document 
	     </div>
	    <form id="FileUploadForm" action="FileUploadServlet" enctype="multipart/form-data" method="post" onsubmit="return form_submit();">
	    
	     <% if (null != myDocList ) { %>
	     <div class=ListItem>
	     	<span class="textSpan">Rely on File :</span>
	     	<input type="hidden" id="_RelyOnFileIDs" name = "_RelyOnFileIDs" value = "" />
	     	<select style="width:305px;   height:23px;"  class="inputSelect" name="_RelyOnFileID">
	     		<option value="-1">&nbsp;&nbsp;&nbsp;======Choose Rely on Module =======&nbsp;&nbsp;&nbsp;</option>
	     <%   	Iterator<ModuleFile> it = refList.iterator();
	             while(it.hasNext()){
	                 ModuleFile  mFile = it.next();
	     %>
	     				<option value="<%=mFile.getFileId()%>@pub"><%=mFile.getFileName() %></option>
	     <%      } %>
	      <%   	Iterator<ModuleDocument> doclist = myDocList.iterator();
	             while(doclist.hasNext()){
	                 ModuleDocument  mFile = doclist.next();
	     %>
	     				<option value="<%=mFile.getDocId() %>@user"><%=mFile.getDocName() %></option>
	     <%      } %>
	     	</select><input type="button" onclick="addRelayFile()" value="add" />
	     	<div id="moreRelay"></div>
	     </div>
	     <%} %>
	     <div class="ListItem">
		      <span class="textSpan">File Upload  :</span>
		      <input style="width:305px;   height:23px;" size=33 type="file" class="inputText" name="xmlDocName" id="xmlDocName" UNSELECTABLE="on" />
		      <input type="button" name="File_Upload" onclick="FileUpload()" value="upload" /> &nbsp; &nbsp; &nbsp;
		      <span style="color:#999;font-size:14px;">(.yin   &nbsp; .yang)</span>
		 </div>
		 <% if(null != request.getAttribute("msgString")) { %>
		 <div class="ListItem" align = "center">
		 	  <span class="noticeSpan"> <%=request.getAttribute("msgString") %> </span>
		 </div>
		 <% } %>
    </form>
    </div>
    <form id="DeleteDocForm" action="DeleteDocumentServlet" method="get">
    	<input type="hidden" id="_docIdForDelete" name="_docIdForDelete" value="" />
   	</form>
    
<script type="text/javascript" >
   function deleteDocment(_docId){
   		if(confirm("This document will be deleted. Continue？"))
 		{
 			document.getElementById("_docIdForDelete").value=_docId;
 			document.getElementById("DeleteDocForm").submit();
		}
   }
   function createNewModule(){
       document.getElementById("CreateNewModuleForm").action = "CreateNewModuleServlet";
       var moduleName = document.getElementById("ModuleName").value;
       if("" == document.getElementById("ModuleName").value){
       		alert("模块名不能为空!");
       		document.getElementById("ModuleName").focus();
       		return;
       }
       if(moduleName.indexOf("zxr10-") != 0){
       		alert("模块名必须以 \"zxr10-\" 开头!");
       		document.getElementById("ModuleName").focus();
       		return;
       }
       if("SubModule" == document.getElementById("_moduleType").value){
       		if("" == document.getElementById("_moduleType").value
       		   || "##" == document.getElementById("_moduleType").value){
       		   	alert("MainModuleName is empty!");
       		 	return;
       		}
       }
       if(nameVerify(document.getElementById("ModuleName").value)){
       		document.getElementById("CreateNewModuleForm").submit();
       }
	   
   }
   var hasMainmodule = false;
   function getModuleType(moduleType){
   		if("" == moduleType || "MainModule" == moduleType) { 
   		    $("#MainModuleSpan").html("");
	    	$("#MainModuleSpan").css("display", "none");
   			return;
   		}
   		$.ajax({
		    //提交数据的类型 POST GET                
			type: "post",
			//返回数据的格式 
			dataType:"xml", //"xml", "html", "script", "json", "jsonp", "text".
			//提交的网址
			"url" : "AjaxGetUserMainModuleServlet",
			//提交的数据
			data: {},
			//在请求之前调用的函数
			beforeSend: function(XMLHttpRequest){
				  //$("#Itme_add").html("<div style='margin 10px' align>数据加载中……</div>");  		 
			},
			//成功返回之后调用的函数
			success: function(data, textStatus){
           /******************************************************************************************/
                 $("#_mainModule").html("");
                 if($(data).find("mainModuleItem").length > 0){
                    hasMainmodule = true;
                    $("#_mainModule").append("<option value ='##'>==Choose The MainModule==</option>");
                 }
                 $(data).find("mainModuleItem").each(function(index, element) {
                        var ModuleName     = $(this).text();
                        $("#_mainModule").append("<option value ='"+ModuleName+"'>"+ModuleName+"</option>");
                 });
            /******************************************************************************************/
			},
			//调用执行后调用的函数
			complete: function(XMLHttpRequest, textStatus){
			    if(hasMainmodule){
					$("#MainModuleSpan").css("display", "");
			    	hasMainmodule = false;
			    }else{
			    	$("#MainModuleSpan").html("");
			    	$("#MainModuleSpan").css("display", "none");
			    }
			},
			//调用出错执行的函数 
			error: function(){
				//请求出错处理
			}
		})
   }
   
   function addRelayFile(){
   		$.ajax({
		    //提交数据的类型 POST GET                
			type: "post",
			//返回数据的格式 
			dataType:"xml", //"xml", "html", "script", "json", "jsonp", "text".
			//提交的网址
			"url" : "AjaxGetRelayModuleServlet",
			//提交的数据
			data: {},
			//在请求之前调用的函数
			beforeSend: function(XMLHttpRequest){
				  //$("#Itme_add").html("<div style='margin 10px' align>数据加载中……</div>");  		 
			},
			//成功返回之后调用的函数
			success: function(data, textStatus){
           /******************************************************************************************/
                 var optionStr = "";
                 $(data).find("docItem").each(function(index, element) {
                        var docId     = $(this).children("docId").text();
                        var docName   = $(this).children("docName").text();
                        optionStr +="<option value ='"+docId+"'>"+docName+"</option>"
                 });
                 $("#moreRelay").append(
                 "<div class = 'ListItem'>"
                 +"<span class='textSpan'>Rely on File :</span>"
                 +"<select style='width:305px;   height:23px;'  class='inputSelect'  name='_RelyOnFileID'>"
                 +"<option value='-1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;======Choose Rely on Module =======</option>"
                 +optionStr
                 +"</select>  "
                 +"<a href='#' class='rangeRemove' onclick='relayDelete(this)' ><img class='buttonBcImg' width='20' src='./images/delete.png' /></a>  "
                 +"</div>");
            /******************************************************************************************/
			},
			//调用执行后调用的函数
			complete: function(XMLHttpRequest, textStatus){
			},
			//调用出错执行的函数 
			error: function(){
				//请求出错处理
			}
		})
   }
   
   function relayDelete(obj){
        $(obj).parent().remove();
   }
   function FileUpload(){
   		var RelyOnFileIDs = document.getElementsByName("_RelyOnFileID");
   		var relayPara="";
   		for(i = 0; i < RelyOnFileIDs.length ; i++){
   			if(RelyOnFileIDs[i].value != "-1"){
   				relayPara +=RelyOnFileIDs[i].value + ",";
   			}
   		}
   		if(RelyOnFileIDs.length > 0){
   			relayPara = relayPara.substring(0,relayPara.length -1);
   		}
   		document.getElementById("_RelyOnFileIDs").value = relayPara;
   		document.getElementById("FileUploadForm").submit();
   		
   }
</script>    
  </body>
</html>
